package com.situ.stmall.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stmall.manager.entity.Category;
import com.situ.stmall.manager.mapper.CategoryMapper;
import com.situ.stmall.manager.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategorySeiviceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 添加
     *
     * @param category
     */
    @Override
    public int add(Category category) throws Exception {
        //参数验证
        if(category.getName() == null || category.getName().length() == 0){
            throw new Exception("分类名不能为空！");
        }
        if(category.getRecom() == null){
            throw  new Exception("推荐不能为空！");
        }
        //逻辑处理
        //访问数据库
        return categoryMapper.insert(category);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public int remove(Integer id) throws Exception {
        if(categoryMapper.selectById(id) == null){
            throw  new Exception("ID不存在！");
        }
        return categoryMapper.delete(id);
    }

    /**
     * 修改
     *
     * @param category
     */
    @Override
    public int edit(Category category) throws Exception {
        if(categoryMapper.selectById(category.getId()) == null){
            throw new Exception("ID不存在！");
        }
        return categoryMapper.update(category);
    }

    /**
     * 查询id
     *
     * @param id
     */
    @Override
    public Category getById(Integer id) {
        return categoryMapper.selectById(id);
    }

    /**
     * 搜索
     *
     * @param category
     */
    @Override
    public List<Category> search(Category category) {
        return categoryMapper.select(category);
    }

    /**
     * 分页
     *
     * @param page
     * @param limit
     * @param category
     */
    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Category category) {
        //1.启动分页
        PageHelper.startPage(page,limit);
        //2.调用Mapper层
        List<Category> list =  categoryMapper.select(category);
        //3.构造pageInfo对象
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
